<?php
/**
 * "实用工具"控制器
 *
 * @author hiscaler <hiscaler@gmail.com>
 * @link http://www.yuandot.com/
 * @copyright Copyright &copy; 2009-2010 Yuandot Information Technology CO.,LTD
 * @license http://doit.yuandot.com/license/
 */

class UtilsController extends Controller {
    public function init() {
        $this->layout = 'contents';
    }
//    public function actionIndex() {
//        $this->render('index');
//    }

    // 清除缓存
    public function actionClearCache() {
        if (Yii::app()->request->isAjaxRequest) {
            $cache_items = Yii::app()->request->getPost('cache_items');
            if ($cache_items == null) {
                echo Yii::t('utils', 'clear_cache_hint_unselected_items');
            } else {
//                CVarDumper::dump($cache_items, 10, true);
                foreach ($cache_items as $item) {
                    $cache = Yii::app()->cache->delete($item);
                }
                echo Yii::t('utils', 'clear_cache_hint_success');
            }
        } else {
            $this->render('clearCache');
        }
    }

    // 数据库备份
    public function actionDBBackup() {
    }

    // 数据转移
    public function actionDatasTransfer() {
        if (Yii::app()->request->isAjaxRequest) {
            $source_object = Yii::app()->request->getParam('source_object');
            $target_object = Yii::app()->request->getParam('target_object');
            if (intval($target_object) == intval($source_object)) {
                echo Yii::t('utils', 'datas_transfer_error_source_value_equal_to_target_value');
            } else {
                $model_source = Node::model()->findByPk($source_object);
                $model_target = Node::model()->findByPk($target_object);
                if (($model_source === null) || ($model_target === null)) {
                    echo Yii::t('common', 'invalid_request');
                } else {
                    $model_type_source = trim($model_source->type);
                    $model_type_target = trim($model_target->type);
                    if (($model_type_source == Node::TYPE_ARTICLE) || $model_type_target) {
                        echo Yii::t('utils', 'datas_transfer_error_disable_model_article', array(
                            'source' => $model_source->name,
                            'target' => $model_target->name,
                        ));
                    } else {
                        if ($model_type_source != $model_type_target) {
                            echo Yii::t('utils', 'datas_transfer_error_source_model_unequal_to_target_model', array(
                                'source' => $model_source->name,
                                'target' => $model_target->name,
                            ));
                        } else {
                            $c = CActiveRecord::model($model_type_source)->updateAll(array(
                                'node_id' => $target_object), "node_id = {$source_object}"
                            );
                            echo Yii::t('utils', 'datas_transfer_affect_records_count', array('count' => $c));
                        }
                    }
                }
            }
        } else {
            $this->render('datasTransfer');
        }
    }
    
// -----------------------------------------------------------
// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}